/* Author: Mark Faust
 *
 * C version of predictor file
*/

#ifndef PREDICTOR_H_SEEN
#define PREDICTOR_H_SEEN

#define TAKEN            true
#define NOT_TAKEN        false

#define TARGET_HISTORY_BUFFER_SIZE	32
#define TARGET_HISTORY_BUFFER_NUM_BITS	13
#define TARGET_HISTORY_BUFFER_MASK	0x00001FFF

#define PREDICTOR_TABLE_SIZE		8192
#define GLOBAL_HISTORY_NUM_BITS		20
#define GLOBAL_HISTORY_MASK		0x000FFFF
#define GLOBAL_HISTORY_UPDATE_MASK_T	0x00000001;
#define GLOBAL_HISTORY_UPDATE_MASK_N	0xFFFFFFFE;

#define HASH_NUM			6
#define HASH_PREDICTOR_COUNTER_BITS	4
#define HASH_PREDICTOR_COUNTER_MASK	0x0000000F
#define HASH_PREDICTOR_COUNTER_MAX	7
#define HASH_PREDICTOR_COUNTER_MIN	-8

#define HASH_PREDICTOR_COUNTER_DEFAULT	(unsigned)HASH_PREDICTOR_COUNTER_MASK/2

#define HASH_PREDICTOR_NUM_BITS		HASH_NUM*HASH_PREDICTOR_COUNTER_BITS
//#define HASH_PREDICTOR_TABLE_SIZE	124
#define HASH_PREDICTOR_TABLE_SIZE	665

#define PREDICTOR_NUM_BITS		2
#define PREDICTOR_COUNTER_MAX		0x00000003
#define PREDICTOR_TAKEN			0x00000002
#define PREDICTOR_NOT_TAKEN		0x00000000
#define PREDICTOR_MASK			0x00000003

//#define DEBUG				0

#include <cstddef>
#include <cstring>
#include <inttypes.h>
#include <vector>
#include <cmath>
#include "op_state.h"   // defines op_state_c (architectural state) class 
#include "tread.h"      // defines branch_record_c class

extern unsigned THB[TARGET_HISTORY_BUFFER_SIZE];
extern unsigned THB_head_index;

extern unsigned predictor_table[PREDICTOR_TABLE_SIZE];
extern int hash_predictor_table[HASH_NUM][HASH_PREDICTOR_TABLE_SIZE];
extern unsigned global_history;

extern unsigned hash_num;
extern unsigned path_num;
extern unsigned predictor_table_index;
extern unsigned hash_predictor_table_index;

unsigned getHashIndex(unsigned global_history);
unsigned getHashResult(unsigned PATH);

class PREDICTOR
{
    public:
        bool get_prediction( const branch_record_c* br, const op_state_c* os );

        void update_predictor( const branch_record_c* br, const op_state_c* os, bool taken );

};

#endif // PREDICTOR_H_SEEN
